package com.weiyuwqj.api.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Copyright 2015-2019 Yunfucloud technology co., Ltd.
 * Created by fuxx on 15/11/16.
 */
public class JsonUtils {
    public static String toJson(Object value) {
        ObjectMapper mapper = new ObjectMapper();
        String json = "";
        try {
            json = mapper.writeValueAsString(value);
        } catch (Exception e) {

        }
        return json;
    }

    /**
     * 将对象转为json，去除null值的key
     *
     * @param value 对象
     * @return json
     */
    public static String toJsonNotNull(Object value) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        String json = "";
        try {
            json = mapper.writeValueAsString(value);
        } catch (Exception e) {

        }
        return json;
    }

    //将json 对象转为Map
    public static Map json2Map(String jsondata) {

        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> map = new HashMap<>();

        try {
            map = mapper.readValue(jsondata, HashMap.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

    //将json对象转为指定对象类型
    public static <T> T json2Object(String jsondata, Class<T> tClass) {

        ObjectMapper mapper = new ObjectMapper();
        //Map<String,Object> map = new HashMap<>();
        T o = null;
        try {
            o = mapper.readValue(jsondata, tClass);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return o;
    }


    public static void main(String[] args) {
        Object[] objs = new Object[3];

        objs[0] = new Integer(1);
        objs[1] = new String("sdsd");
        objs[2] = new BigDecimal(121);


        String str = JsonUtils.toJson(objs);
        System.out.println(str);

    }

}
